package com.supor.aiot.mine.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.baseconfig.common.utils.SystemUtils;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.utils.SoftKeyBoardListener;
import com.supor.aiot.mine.R;

import androidx.annotation.NonNull;

public class EditSignatureDialog extends Dialog {

    public EditSignatureDialog(@NonNull Context context) {
        super(context);
    }

    public EditSignatureDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusTextColorDark(this);
    }

    public static void setStatusTextColorDark(Dialog dialog) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            dialog.getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            dialog.getWindow().setStatusBarColor(Color.BLACK);
        }
    }

    public static class Builder {

        private Context context;
        private View layout = null;
        private String title;
        private String editValue;
        private static final int edtTextMaxLength = 30;
        private String positiveButtonText;
        private String negativeButtonText;

        private OnClickListener positiveButtonClickListener;

        private OnClickListener negativeButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        public EditSignatureDialog.Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        public EditSignatureDialog.Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public String getEditValue() {
            return editValue;
        }

        public void setEditValue(String editValue) {
            this.editValue = editValue;
        }

        public EditSignatureDialog.Builder setPositiveButton(int positiveButtonText, OnClickListener listener) {
            this.positiveButtonText = (String) context.getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public EditSignatureDialog.Builder setPositiveButton(String positiveButtonText, OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public EditSignatureDialog.Builder setNegativeButton(int negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = (String) context.getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public EditSignatureDialog.Builder setNegativeButton(String negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public EditSignatureDialog create() {
            LayoutInflater inflater =
                    (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final EditSignatureDialog dialog = new EditSignatureDialog(context, R.style.appBasicDialog);
            layout = inflater.inflate(R.layout.dialog_edit_signature, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));

            WindowManager.LayoutParams p = dialog.getWindow().getAttributes();
            int widthDp = AppAgent.getScreenWidth();
            int height = context.getResources().getDimensionPixelSize(R.dimen.dp_300);
            p.width = widthDp;
            p.height = height;
            dialog.getWindow().setAttributes(p);
            dialog.getWindow().setGravity(Gravity.BOTTOM);

            SoftKeyBoardListener.setListener(layout, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
                @Override
                public void keyBoardShow(int height) {
                    WindowManager.LayoutParams p = dialog.getWindow().getAttributes();
                    p.height += height;
                    dialog.getWindow().setAttributes(p);
                }

                @Override
                public void keyBoardHide(int height) {
                    WindowManager.LayoutParams p = dialog.getWindow().getAttributes();
                    p.height = context.getResources().getDimensionPixelSize(R.dimen.dp_300);
                    dialog.getWindow().setAttributes(p);
                }
            });

            dialog.getWindow().setGravity(Gravity.BOTTOM);
            dialog.getWindow().setWindowAnimations(R.style.bottom_window_anim);

            TextView titleText = ((TextView) layout.findViewById(R.id.dialog_title));
            titleText.setText(title);

            final EditText inputText = ((EditText) layout.findViewById(R.id.dialog_edit));
            final TextView tvCount = ((TextView) layout.findViewById(R.id.input_count));

            inputText.setText(editValue);
            tvCount.setText((editValue.trim().length() + "/" + edtTextMaxLength));

            inputText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(edtTextMaxLength)});
            inputText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    tvCount.setText((inputText.getText().toString().trim().length() + "/" + edtTextMaxLength));
                }
            });

            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positive_btn)).setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    layout.findViewById(R.id.positive_btn).setOnClickListener(v -> {
                        SystemUtils.hideInputSoftFromWindowMethod(AppAgent.getApp(),
                                layout.findViewById(R.id.positive_btn));
                        editValue = inputText.getText().toString();
                        positiveButtonClickListener.onClick(dialog,
                                DialogInterface.BUTTON_POSITIVE);
                    });
                }
            } else {
                layout.findViewById(R.id.positive_btn).setVisibility(View.GONE);
                layout.findViewById(R.id.btn_vertical_line).setVisibility(View.GONE);
            }
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.negative_btn)).setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    layout.findViewById(R.id.negative_btn)
                            .setOnClickListener(v -> {
                                SystemUtils.hideInputSoftFromWindowMethod(AppAgent.getApp(),
                                        layout.findViewById(R.id.negative_btn));
                                negativeButtonClickListener
                                        .onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                            });
                }
            } else {
                layout.findViewById(R.id.negative_btn).setVisibility(View.GONE);
                layout.findViewById(R.id.btn_vertical_line).setVisibility(View.GONE);
            }

            dialog.setContentView(layout);
            return dialog;
        }
    }
}
